<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 3/12/14
 * Time: 9:35 PM
 */


Yii::import('zii.widgets.CMenu');

class QMenu extends CMenu
{

    public $multi = false;
    private $currentLang = '';

    /**
     * Initialize the widget
     */
    public function init()
    {
        //set language menu
        $this->currentLang = Yii::app()->language;
        //parent::init();
    }

    public function run()
    {
        if (!$this->multi)
            $this->renderCustomNormalMenu();
        //parent::run();
    }

    /*
     * not top menu generate menu style
     */
    private function renderMenuRecursiveMulti()
    {
        $query = "SELECT   MENU_CODE,"
            . "         FN_QTC_MULTI_NAME(MENU_NAME, MENU_ENG_NAME, :LANG_CODE) AS MENU_NAME, "
            . "         PARENT_MENU_CODE, "
            . "         SORT_ORDER, "
            . "         USE_YN "
            . "         FROM TB_QTC_MENU"
            . "         WHERE USE_YN = 'Y' "
            . "         ORDER BY SORT_ORDER ";

        $command = Yii::app()->db->createCommand($query);
        $results = $command->queryAll(true, array(':LANG_CODE' => $this->currentLang));

        $this->renderCustomMenuMulti($results);

    }

    // render menu multi
    private function renderCustomMenuMulti($results)
    {
        $data = $this->buildTreeArray($results);
        $html = '<ul>';
        $html .= $this->buildTreeHtml($data);
        $html .= '</ul>';
        echo $html;

    }

    // render menu multi
    private function renderCustomNormalMenu()
    {
        $query = "SELECT   MENU_CODE,"
            . "             FN_QTC_MULTI_NAME(MENU_NAME, MENU_ENG_NAME, :LANG_CODE) AS MENU_NAME, "
            . "             PARENT_MENU_CODE, "
            . "             SORT_ORDER, "
            . "             USE_YN "
            . "             FROM TB_QTC_MENU"
            . "          WHERE USE_YN = 'Y' "
            . "          AND PARENT_MENU_CODE IS NULL"
            . "          ORDER BY SORT_ORDER ";
        $command = Yii::app()->db->createCommand($query);
        $results = $command->queryAll(true, array(':LANG_CODE' => $this->currentLang));
        // open tag li
        echo CHtml::openTag('div');
        // get second menu
        foreach ($results as $result) {
            $query = "SELECT   MENU_CODE,"
                . "         FN_QTC_MULTI_NAME(MENU_NAME, MENU_ENG_NAME, :LANG_CODE) AS MENU_NAME, "
                . "         PARENT_MENU_CODE, "
                . "         SORT_ORDER, "
                . "         USE_YN "
                . "         FROM TB_QTC_MENU "
                . "         WHERE USE_YN = 'Y' "
                . "         AND PARENT_MENU_CODE = :MENU_CODE "
                . "         ORDER BY SORT_ORDER ";
            $command = Yii::app()->db->createCommand($query);
            $childItems = $command->queryAll(true, array(':LANG_CODE' => $this->currentLang, ':MENU_CODE' => $result['MENU_CODE']));
            //start render menu
            echo CHtml::openTag('div', array('class' => 'column'));
            echo CHtml::openTag('a', array('href' => 'dad.html'));
            echo $result['MENU_NAME'];
            echo CHtml::closeTag('a') . "\n";
            //begin render level 2
            if (!empty($childItems)) {
                echo CHtml::openTag('div');
                echo CHtml::openTag('ul');
                foreach ($childItems as $childItem) {
                    echo CHtml::openTag('li');
                    echo CHtml::openTag('a', array('href' => 'dad.html'));
                    echo $childItem['MENU_NAME'];
                    echo CHtml::closeTag('a') . "\n";
                    echo CHtml::closeTag('li') . "\n";
                }
                echo CHtml::closeTag('ul') . "\n";

                echo CHtml::closeTag('div') . "\n";
            }
            //end render level 2
            echo CHtml::closeTag('div') . "\n";
            //end render menu
        }
        echo CHtml::closeTag('div') . "\n";
    }

    // get tree array
    private function buildTreeArray(array $elements, $parentId = null)
    {
        $branch = array();

        foreach ($elements as $element) {
            if ($element['PARENT_MENU_CODE'] == $parentId) {
                $children = self::buildTreeArray($elements, $element['MENU_CODE']);
                if ($children) {
                    $element['CHILDREN'] = $children;
                }
                $branch[] = $element;
            }
        }

        return $branch;
    }

    // get tree html
    private function buildTreeHtml(array $elements)
    {
        $html = "";

        foreach ($elements as $element) {
            if (isset($element['CHILDREN']) && sizeof($element['CHILDREN']) > 0) {
                $html .= '<li><a>' . $element['MENU_NAME'] . '</a>';
                $html .= '<ul>';
                $html .= self::buildTreeHtml($element['CHILDREN']);
                $html .= '</ul>';
                $html .= '</li>';
            } else {
                $html .= '<li><a >' . $element['MENU_NAME'] . '</a></li>';
            }
        }

        return $html;
    }

} 